package com.dha.caodangcongnghe;

import com.dha.asynctask.XemThoiKhoaBieuAsyncTask;
import com.dha.status.NetworkStatus;
import com.dha.ultility.QuangCao;
import com.dha.variable.ThongTinTaiKhoan;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class XemThoiKhoaBieu extends ActionBarActivity {
	private String maSo = null;
	private NetworkStatus networkStatus;
	

	// private QuangCao quangCao;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_thoikhoabieu);

		// quangCao = new QuangCao(this, XemThoiKhoaBieu.this);
		
		
		Intent i = getIntent();
		maSo = i.getStringExtra("MaSo");
		networkStatus = new NetworkStatus(getBaseContext());
		if (networkStatus.isConnected()) {
			XemThoiKhoaBieuAsyncTask asyncTask = new XemThoiKhoaBieuAsyncTask(
					XemThoiKhoaBieu.this, maSo);
			asyncTask.execute();
		} else {
			Intent intentLoi = new Intent("SuCoVeKetNoiMang");
			startActivity(intentLoi);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		Log.i("hehe","nnnnnnnnnnn");
		getMenuInflater().inflate(R.menu.menu_xemthoikhoabieu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.mn_XemTKB:
			// get layouy_dianlog_nha_ma_so_sinh_vien
			LayoutInflater inflater = LayoutInflater.from(this);
			View nhapMaSinhVienView = inflater.inflate(
					R.layout.layout_dialog_nhap_ma_so_sinh_vien, null);

			AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
			// // set layouy_dianlog_nha_ma_so_sinh_vien.xml to alertdialog
			// builder
			alertBuilder.setView(nhapMaSinhVienView);

			final EditText et_maSinhVien = (EditText) nhapMaSinhVienView
					.findViewById(R.id.et_layout_nhapMaSinhVien);

			// set dialog message

			alertBuilder.setCancelable(false)
					.setNegativeButton("OK", new OnClickListener() {
						// Xu ly nut ok
						@Override
						public void onClick(DialogInterface dialog, int which) {
							// neu nhap so khac 12 chu so ==> thong bao loi

							if (et_maSinhVien.getText().length() == 12
									|| et_maSinhVien.getText().length() == 7) {
								
								// set gia tri vao masinh vien

								// Goi inten xem diem
								if (networkStatus.isConnected()) {
									XemThoiKhoaBieuAsyncTask asyncTask = new XemThoiKhoaBieuAsyncTask(
											XemThoiKhoaBieu.this, et_maSinhVien
													.getText().toString());
									asyncTask.execute();
								} else {
									Intent intentLoi = new Intent(
											"SuCoVeKetNoiMang");
									startActivity(intentLoi);
								}
								// XemThoiKhoaBieuAsyncTask asyncTask = new
								// XemThoiKhoaBieuAsyncTask(
								// XemThoiKhoaBieu.this, maSo);
								// asyncTask.execute();
							} else {
								Toast.makeText(
										getBaseContext(),
										"Mã sinh viên 12 kí tự\nMã giáo viên 7 kí tự",
										Toast.LENGTH_SHORT).show();
							}

						}
					}).setPositiveButton("Thoát ra", new OnClickListener() {
						// Xu ly nut nhap lai
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.cancel();
						}
					}).setTitle("Nhập mã số sinh viên/giáo viên");

			// create alert dialog
			AlertDialog alertDialog = alertBuilder.create();

			// show it
			alertDialog.show();
			break;
		}
		return super.onOptionsItemSelected(item);
	}

}
